class PublicController < ApplicationController

  layout 'public'
  
  before_filter :authorize_public_access, :except => [:login, :send_login, :index, :register, :create]

  def index
  end
  
  def login
    @user = User.new
  end
  
  def send_login
    @user = User.new(params[:user])
    logged_in_user = @user.try_to_login
    if logged_in_user
      session[:user_id] = logged_in_user.id
      session[:user_level] = logged_in_user.level
      flash[:notice] = "You are now logged in."
      redirect_to(:action => 'index')
    else
      flash[:notice] = "Username/password combination incorrect. Please make sure your caps lock key is off and try again."
      redirect_to(:action => 'index')
    end
  end
  
  def logout
    session[:user_id] = nil
    session[:user_level] = nil
    flash[:notice] = "You are now logged out."
    redirect_to(:action => 'index')
  end
  
  def register
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    @password_repeat = @user.password_repeat
    @password = @user.password
    if @password == @password_repeat && @user.save
      flash[:notice] = 'User was successfully created.'
      redirect_to :action => 'index'
    else
      flash[:notice] = 'Passwords have to be identical.'
      render :action => 'register'
    end
  end

end
